package com.cros.mysql;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class ServiceDataSource {
	private static volatile ServiceDataSource instance=null;
	public static  DataSource dataSource;
	private String url;
	
	public static ServiceDataSource getInstance(){
		if(instance==null){
			synchronized(ServiceDataSource.class){
				if(instance==null){
					instance= new ServiceDataSource();
				}
			}
		}
		return instance;
	}
	
	private ServiceDataSource(){
	    BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        ds.setUsername("aseval2");
        ds.setPassword("sqlserver2008$");
        ds.setUrl("jdbc:sqlserver://192.168.0.130:1433;DatabaseName=recordaseval");
        ds.setMaxActive(3000);
        ds.setInitialSize(3000);
        ds.setValidationQuery("select 1");
        dataSource=ds;
	}
	
	//datasource_connection_pool.properties
	private ServiceDataSource(String pathProperties) throws Throwable{
		Properties propiedades = new Properties();
		propiedades.load(new FileInputStream(pathProperties));
		dataSource = BasicDataSourceFactory.createDataSource(propiedades);
	}
	
	  public static void printDataSourceStats(DataSource ds) {
	        BasicDataSource bds = (BasicDataSource) ds;
	        System.out.println("NumActive: " + bds.getNumActive());
	        System.out.println("NumIdle: " + bds.getNumIdle());
	    }

	    public static void shutdownDataSource(DataSource ds) throws SQLException {
	        BasicDataSource bds = (BasicDataSource) ds;
	        bds.close();
	    }
	
	

}
